<?php

namespace App\Http\Controllers\Admin\Site;

use App\B2BProject\Admin\AdminControllerBuildTrait;
use App\B2BProject\Site\ArticleCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    use AdminControllerBuildTrait;

    protected $moduleName = '分类管理';

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //读取值
        $all = ArticleCategory::where('parent_id', 0)->get();
        return view('admin/site/category/index')->with('categories', $all);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //读取值
        $all = ArticleCategory::where('parent_id', 0)->get();

        return view('admin/site/category/create')->with('categories', $all);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $re = ArticleCategory::create([
            'name'         => "$request->name",
            'display_name' => "$request->display_name",
        ]);

        if (($parent_id = $request->input('parent_id')) > 0) {
            $parent = ArticleCategory::findOrFail($parent_id);

            $parent->children()->save($re);
        }

        return redirect()->route('admin.category.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // 根据id查找分类信息
        $editTarget = ArticleCategory::findOrFail($id);
        $all        = ArticleCategory::where('parent_id', 0)->get();
        return view('admin/site/category/edit')->with('editTarget', $editTarget)->with('categories', $all);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int                      $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $affect = ArticleCategory::findOrFail($id);

        $affect->name         = $request->input('name');
        $affect->display_name = $request->input('display_name');
        $affect->parent_id    = $request->input('parent_id');

        $affect->save();

        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $category = ArticleCategory::findOrFail($id);

        $category->articles()->delete();
        $category->delete();

        return redirect()->back();

    }
}
